package com.xiaoma.listener;

import com.rabbitmq.client.Channel;
import com.xiaoma.service.GoodsService;
import entity.Payorder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.Message;
import java.io.IOException;

/**
 * @Author: xiaoma
 * @Date: 2021/7/16 14:45
 * @description:
 */
@Component
@RabbitListener(queues = {"stock.release.stock.queue"})
@Slf4j
public class StockReleaseListener {
    @Autowired
    private GoodsService goodsService;
    @RabbitHandler
    public void handleStockLockedRelease(Payorder payorder, Message message, Channel channel) throws IOException {
        log.info("*************收到库存解锁的消息***************");
        try {
            //库存解锁
            goodsService.unlockStock(payorder);
            // 手动确认消息消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
